//var host = 'http://union.eeaassyy.com/';
 //var host = 'https://xhwapplet.eeaassyy.com/';
var host = 'http://127.0.0.3/';
var img_add = host + 'uploads/';
var error_img = img_add + 'default.png';
var url = host + 'uinterface.php';
var type_arr = ['', '优惠', '', '', '', '团购', '砍价'];
var local_add = "../../res/";

App({
  data:{
    
  },
  onLaunch: function () {
    return false; // lrxin
    this.data = {
      host: host,
      img_add: img_add,
      error_img: error_img,
      url: url,
      type_arr: type_arr,
      local_add: local_add,
    };
    var key = wx.getStorageSync('userinfo');
    if (!key || JSON.stringify(key) == '{}' || key == '') {
      wx.login({
        success: function (res) {
          post({
            code: 20011,
            js_code: res.code
          }, function (e) {
            if (+e.status == 1) {
              setl('userinfo', e.data.info);
            } else if (+e.status == 15) {
              var openid = e.openid;
              wx.getSetting({
                success: res => {
                  if (res.authSetting['scope.userInfo']) {
                    // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
                    wx.getUserInfo({
                      success: res => {
                        var info = res.userInfo;
                        post({
                          code: 20007,
                          nickname: info.nickName,
                          sex: info.gender,
                          headimgurl: info.avatarUrl,
                          openid: openid,
                        }, function (e) {
                          setl('userinfo', e.data.info);
                        })
                      }
                    })
                  }
                }
              })

            } else {
              alt(e.message);
            }
          }, false)
        }
      })
    }
  },
  globalData: {
    userInfo: null,
    config:null
  },
  p:function(e){
    p(e);
  },
  setl: function (key, val) {
    return setl(key,val);
  },
  getl: function (key) {
    return getl(key);
  },
  alt:function(title){
    alt(title)
  },
  post:function(data,succ){
    post(data,succ)
  },
  empty: function (data, succ) {
    empty(key)
  },

  //lrxin 
  setTitle:function(t){
    if ( typeof (t) != 'string'){ return false;}
    wx.setNavigationBarTitle({
      title: t,
      success:function(){},
      fail:function(){},
      complete:function(){}
    })

  },
})

var post = function(data, succ, option = true) {
  wx.request({
    url: url,
    method: 'POST',
    header: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data:data,
    success: function(e){
      switch(+e.data.status){
        case 1:
          succ(e.data);
        break;

        default:
          if (option){
            alt(e.data.message);
          }else{
            succ(e.data);
          }
          
        break;
      }
    },
    error:function(){
      alt('通信错误 请检查网络连接');
    }
  })
}

var alt = function(title){
  wx.showToast({
    title: title,
    icon: 'none',
  })
}

var p = function(e){
  console.log(e)
}

var setl = function(key,val){
  return wx.setStorageSync(key, val);
}

var getl = function(key){
  return wx.getStorageSync(key);
} 

var empty = function(key){
  if (!key || JSON.stringify(key) == '{}' || key == '' ){
    return true;
  }else{
    return false;
  }
}

